<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="76px"
    >
      <el-form-item label="商家名称" prop="merchantName">
        <el-input
          v-model="queryParams.merchantName"
          placeholder="请输入商家名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营业执照" prop="businessLicense">
        <el-input
          v-model="queryParams.businessLicense"
          placeholder="请输入企业营业执照号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="contanctPhone">
        <el-input
          v-model="queryParams.contanctPhone"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="merchantStatus">
        <template>
          <el-select
            v-model="queryParams.merchantStatus"
            placeholder="请选择状态"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="状态" prop="openStatus">
        <template>
          <el-select v-model="queryParams.openStatus" placeholder="请选择状态">
            <el-option
              v-for="item in optionsPen"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['business:merchant:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['business:merchant:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['business:merchant:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:merchant:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="merchantList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="商家名称"
        align="center"
        prop="merchantName"
        show-overflow-tooltip
      />
      <el-table-column
        label="商家管理员"
        align="center"
        prop="merchantManger"
        show-overflow-tooltip
      />
      <el-table-column
        label="企业营业执照号"
        align="center"
        prop="businessLicense"
        show-overflow-tooltip
      />
      <el-table-column
        label="联系方式"
        align="center"
        prop="contanctPhone"
        show-overflow-tooltip
      />
      <el-table-column
        label="地址"
        align="center"
        prop="merchantAddress"
        show-overflow-tooltip
      />
      <el-table-column
        label="营业执照"
        align="center"
        prop="businessPicture"
        width="80"
      >
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.businessPicture"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="经营范围描述"
        align="center"
        prop="description"
        show-overflow-tooltip
      />
      <el-table-column label="审核状态" align="center" prop="merchantStatus">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.merchantStatus == 0"
            effect="dark"
            type="warning"
            >待审核</el-tag
          >
          <el-tag
            v-else-if="scope.row.merchantStatus == 1"
            effect="dark"
            type="success"
            >通过</el-tag
          >
          <el-tag
            v-else-if="scope.row.merchantStatus == 2"
            effect="dark"
            type="danger"
            >拒绝</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="openStatus">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.openStatus == 0 && scope.row.merchantStatus == 1"
            effect="dark"
            type="success"
            >启用</el-tag
          >
          <el-tag
            v-if="scope.row.openStatus == 1 && scope.row.merchantStatus == 1"
            effect="dark"
            type="danger"
            >停用</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="
              scope.row.merchantStatus == 0 || scope.row.merchantStatus == 2
            "
            type="primary"
            size="small"
            @click="handleAudit(scope.row)"
            v-hasPermi="['business:deliveryman:edit']"
            >审核</el-button
          >
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:merchant:edit']">修改</el-button> -->
          <el-button
            v-if="scope.row.merchantStatus == 1"
            type="primary"
            size="small"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:merchant:remove']"
          >
            {{ scope.row.openStatus === "0" ? "停用" : "启用" }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 审核商家入驻信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="openAudit"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核" prop="merchantStatus">
          <template>
            <el-select
              style="width: 100%"
              v-model="form.merchantStatus"
              placeholder="请选择状态"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>

        <el-form-item
          label="拒绝原因"
          prop="auditDesc"
          v-if="form.merchantStatus == 2"
        >
          <el-input
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 10 }"
            placeholder="请输入拒绝原因"
            v-model="form.auditDesc"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMerchant,
  getMerchant,
  delMerchant,
  addMerchant,
  updateMerchant,
  openStatusMerchant,
} from "@/api/business/merchant";

export default {
  name: "Merchant",
  data() {
    return {
      options: [
        { value: "0", label: "待审核" },
        { value: "1", label: "通过" },
        { value: "2", label: "拒绝" },
      ],
      optionsPen: [
        { value: "0", label: "启用" },
        { value: "1", label: "停用" },
      ],
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家入驻信息表格数据
      merchantList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openAudit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        merchantName: null,
        merchantManger: null,
        businessLicense: null,
        contanctPhone: null,
        merchantAddress: null,
        businessPicture: null,
        describe: null,
        merchantStatus: null,
        openStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        auditDesc: [
          { required: true, message: "请输入拒绝原因", trigger: "blur" },
        ],
        merchantStatus: [
          {
            required: true,
            message: "请选择审核状态",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商家入驻信息列表 */
    getList() {
      this.loading = true;
      listMerchant(this.queryParams).then((response) => {
        this.merchantList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openAudit = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        auditDesc: "",
        merchantStatus: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商家入驻信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMerchant(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商家入驻信息";
      });
    },
    handleAudit(row) {
      this.reset();
      const id = row.id || this.ids;
      getMerchant(id).then((response) => {
        this.form = response.data;
        this.openAudit = true;
        this.title = "审核商家入驻";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMerchant(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.openAudit = false;
              this.getList();
            });
          } else {
            addMerchant(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let text = "";
      let status = "";
      if (row.openStatus === "0") {
        text = "是否确认停用该商家";
        status = "1";
      } else {
        text = "是否确认启用该商家";
        status = "0";
      }
      this.$modal
        .confirm(text)
        .then(function () {
          return openStatusMerchant({ id: row.id, openStatus: status });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("修改成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/merchant/export",
        {
          ...this.queryParams,
        },
        `merchant_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
